package hpu.e4_demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import hpu.e4_demo.common.Result;
import hpu.e4_demo.entity.TUserInfo;
import hpu.e4_demo.mapper.TUserInfoMapper;
import hpu.e4_demo.service.ITUserInfoService;
import hpu.e4_demo.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author FFF
 * @since 2025-06-21
 */
@RestController
@RequestMapping("/tUserInfo")
public class TUserInfoController {
    @Autowired
    private ITUserInfoService tUserInfoService;
    @Autowired
    private TUserInfoMapper tUserInfoMapper;

    @RequestMapping("/getAllUserInfo")
    public Result<List<TUserInfo>> getAllUserInfo(Integer currentPage, Integer size) {
        return tUserInfoService.getAllUserInfo(currentPage,size);
    }

    //请勿调用此函数，用不到
    @RequestMapping("/addUserInfo")
    public Result<String> addUserInfo(@RequestBody TUserInfo tUserInfo) {
        return tUserInfoService.addUserInfo(tUserInfo);
    }

    @RequestMapping("/modifyUserInfo")
    public Result<String> modifyUserInfo(@RequestBody TUserInfo tUserInfo) {

        return tUserInfoService.modifyUserInfo(tUserInfo);
    }

    @RequestMapping("/deleteUserInfo")
    public Result<String> deleteUserInfo(String userName) {
        return tUserInfoService.deleteUserInfo(userName);
    }

    @RequestMapping("/selectById")
    public Result<List<TUserInfo>> selectById(String userName,String userTrueName,String userPhoneNumber) {
        return tUserInfoService.getById(userName,userTrueName,userPhoneNumber);
    }

    @RequestMapping("/getUserInfo")
    public TUserInfo getUserInfo(@RequestHeader(name = "Authorization") String token) {
        Map<String, Object> map = new HashMap<>();
        map = JwtUtil.parseToken(token);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_name",map.get("username"));
        TUserInfo userInfo = tUserInfoMapper.selectOne( wrapper);
        /*System.out.println(map);*/
        return userInfo;
    }

    @RequestMapping("/updateUserPassword")
    public Result<String> updateUserPassword(String userName, String oldPassword, String newPassword) {
        return tUserInfoService.updateUserPassword(userName,oldPassword,newPassword);
    }

    @RequestMapping("/setRegister")
    public Result<String> setRegister(String userName, String role) {
        return tUserInfoService.setRegister(userName,role);
    }


}
